package com.scorpio.goods.service.impl;

import com.scorpio.basic.dto.OssDatumDTO;
import com.scorpio.basic.enums.GoodsImageTypeEnum;
import com.scorpio.basic.service.OssDatumService;
import com.scorpio.common.exception.CustomException;
import com.scorpio.common.service.impl.BaseServiceImpl;
import com.scorpio.common.utils.ConvertUtils;
import com.scorpio.goods.domain.GoodsEntity;
import com.scorpio.goods.dto.*;
import com.scorpio.goods.mapper.GoodsMapper;
import com.scorpio.goods.service.GoodsService;
import com.scorpio.goods.service.GoodsSkuService;
import com.scorpio.goods.service.GoodsSkuSpecService;
import com.scorpio.goods.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends BaseServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {

    @Autowired
    private OssDatumService ossDatumService;

    @Autowired
    private GoodsSkuService skuService;

    @Autowired
    private GoodsSkuSpecService skuSpecService;

    @Override
    public GoodsEntity selectById(Long id) {
        return baseDao.selectById(id);
    }

    @Override
    public List<GoodsItemDTO> goodsList(GoodsVo vo) {
        List<GoodsEntity> list = baseDao.appList(
                vo.getCategoryId()
                , vo.getQueryText()
                , vo.getQueryType()
                , vo.getQueryMinPrice()
                , vo.getQueryMaxPrice()
                , vo.getOffset()
                , vo.getLimit()
        );

        List<GoodsItemDTO> dtoList = ConvertUtils.sourceToTarget(list, GoodsItemDTO.class);
        for (GoodsItemDTO dto : dtoList) {
            List<OssDatumDTO> imageOss = ossDatumService.selectList(GoodsImageTypeEnum.Image.getType(), dto.getId());
            dto.setImageOss(imageOss);
        }
        return dtoList;
    }

    @Override
    public GoodsDetailsDTO details(Long id) {
        GoodsEntity entity = baseDao.details(id);
        if (entity == null) {
            throw new CustomException("商品不存在或已下架");
        }
        GoodsDetailsDTO details = ConvertUtils.sourceToTarget(entity, GoodsDetailsDTO.class);
        List<OssDatumDTO> imageOss = ossDatumService.selectList(GoodsImageTypeEnum.Image.getType(), details.getId());
        List<OssDatumDTO> detailsOss = ossDatumService.selectList(GoodsImageTypeEnum.Details.getType(), details.getId());
        details.setImageOss(imageOss);
        details.setDetailsOss(detailsOss);
        details.setSkuList(skuList(entity.getId()));
        return details;
    }

    @Override
    public int updateInventory(Long id, Integer change) {
        return baseDao.updateInventory(id, change);
    }

    @Override
    public List<GoodsSkuSpecDTO> skuList(Long goodsId) {
        List<GoodsSkuSpecDTO> list = ConvertUtils.sourceToTarget(skuService.selectByGoods(goodsId), GoodsSkuSpecDTO.class);
        for (GoodsSkuSpecDTO dto : list) {
            dto.setSpecNameList(ConvertUtils.sourceToTarget(skuSpecService.selectSkuNameBySku(dto.getId()), GoodsSpecNameDTO.class));
        }
        return list;
    }
}
